package com.ml.loan.common.utils;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.RandomUtil;
import com.google.common.base.Splitter;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 通用工具
 *
 * @Author Mickey
 * @Date 2021/3/4 0:06
 **/
public enum CommonUtil {

    INSTANCE;

    /**
     * -------------------------------------订单号生成----------------------------------------------------------------------
     */
    public String createOrderNo() {
        //生成19位雪花算法ID  1367148865496682504
        return IdUtil.getSnowflake(1, 1).nextIdStr();
    }

    public String createOrderNoByDate() {
        //生成27位日期+雪花算法ID  202103041367148865496682504
        return createOrderNo(LocalDateTimeUtil.format(LocalDateTime.now(), DatePattern.PURE_DATE_PATTERN));
    }

    public String createOrderNo(String prefix) {
        //生成前缀+雪花算法ID  PRE1367150012894679058
        return prefix + createOrderNo();
    }

    public String createSerialNum(String prefix, Long businessId) {
        //生成前缀 + 15位序列号（纳秒 + 唯一业务ID） 可通过businessId避免极端情况下的重复
        return prefix + (Long.valueOf(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyMMddHHmmssSSS"))) + businessId);
    }

    /**
     * -------------------------------------短信验证码生成----------------------------------------------------------------------
     */
    public String createSmsCode4() {
        //生成4位验证码  4166
        return RandomUtil.randomNumbers(4);
    }

    public String createSmsCode6() {
        //生成6位验证码  490730
        return RandomUtil.randomNumbers(6);
    }

    /**
     * -------------------------------------版本号比较----------------------------------------------------------------------
     */
    public int compareVersion(String ver1, String ver2) {
        if (ver1 == null && ver2 == null) {
            throw new RuntimeException("版本号不能都为空");
        }
        if (ver1 == null) {
            return -1;
        }
        if (ver2 == null) {
            return 1;
        }
        if (ver1.equals(ver2)) {
            return 0;
        }
        String[] version1 = ver1.split("\\.");
        String[] version2 = ver2.split("\\.");
        String defValue = "0000000000";
        String format = "%" + defValue.length() + "s";
        StringBuilder ver1Builder = new StringBuilder(version1.length * 10);
        StringBuilder ver2Builder = new StringBuilder(version2.length * 10);
        if (version1.length > version2.length) {
            for (int i = 0; i < version1.length; i++) {
                ver1Builder.append(String.format(format, version1[i]).replace(' ', '0'));
                ver2Builder.append(version2.length > i ? String.format(format, version2[i]).replace(' ', '0') : defValue);
            }
        } else if (version1.length < version2.length) {
            for (int i = 0; i < version2.length; i++) {
                ver2Builder.append(String.format(format, version2[i]).replace(' ', '0'));
                ver1Builder.append(version1.length > i ? String.format(format, version1[i]).replace(' ', '0') : defValue);
            }
        } else {
            for (int i = 0; i < version2.length; i++) {
                ver1Builder.append(String.format(format, version1[i]).replace(' ', '0'));
                ver2Builder.append(String.format(format, version2[i]).replace(' ', '0'));
            }
        }
        return ver1Builder.toString().compareTo(ver2Builder.toString());
    }


    /**
     * 字符串切割为list整型
     *
     * @param str
     * @return
     */
    public List<Integer> splitToListInt(String str) {
        if (StringUtils.isBlank(str)) {
            return Lists.newArrayList();
        }
        List<String> strList = Splitter.on(",").trimResults().omitEmptyStrings().splitToList(str);
        List<Integer> list = strList.stream().map(strItem -> Integer.parseInt(strItem)).collect(Collectors.toList());
        return list;
    }
}
